<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>用户管理-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../static/css/font.css">
		<link rel="stylesheet" href="../../static/css/weadmin.css">
		<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
		<!--[if lt IE 9]>
	      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
	      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
	    <![endif]-->
	</head>

	<body>
		<div class="weadmin-body">
			<form id="user_form" action="" method="post" class="layui-form layui-form-pane">
				<div class="layui-form-item">
					<label for="user_account" class="layui-form-label">
                        <span class="we-red">*</span>账号
                    </label>
					<div class="layui-input-inline">
						<input type="text" id="user_account" name="account" required="" lay-verify="required" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item" id="user_password_div">
					<label for="user_password" class="layui-form-label">
                        <span class="we-red">*</span>密码
                    </label>
					<div class="layui-input-inline">
						<input type="password" id="user_password" name="password" required="" lay-verify="required" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="user_nickname" class="layui-form-label">
                        <span class="we-red">*</span>昵称
                    </label>
					<div class="layui-input-inline">
						<input type="text" id="user_nickname" name="nickname" required="" lay-verify="required" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="user_tel" class="layui-form-label">
                        电话
                    </label>
					<div class="layui-input-inline">
						<input type="text" id="user_tel" name="tel" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="user_note" class="layui-form-label">
                       备注
                    </label>
					<div class="layui-input-inline">
						<input type="text" id="user_note" name="note" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="user_status" class="layui-form-label">
                        <span class="we-red">*</span>用户状态
                    </label>
					<div class="layui-input-inline">
						<select id="role_status" name="status" class="valid">
							<option value="1">正常</option>
							<option value="0">禁用</option>
						</select>
					</div>
				</div>
				<div class="layui-form-item layui-form-text">
					<label class="layui-form-label">
                        所属角色
                    </label>
					<div class="layui-input-block" id="role_checkbox">
					</div>
				</div>
				<div class="layui-form-item">
					<button class="layui-btn" lay-submit="" lay-filter="add">提交</button>
				</div>
			</form>
		</div>
		<script src="../../lib/layui/layui.js" charset="utf-8"></script>
		<!--<script src="../../static/js/admin.js" charset="utf-8"></script>-->
		<script src="../../publicjs/jquery-1.8.0.min.js" charset="utf-8"></script>
		<script src="../../publicjs/smallkingutil.js" charset="utf-8"></script>
		<script type="text/javascript">
			layui.use(['form', 'layer'], function() {
				var form = layui.form,
					layer = layui.layer;
				// 先判断当前的操作是新增还是修改
				var userId = commonUtil.getRequestParam("userId");
				if(userId) {
					// 请求后台得到当前修改的用户数据，将其显示在表单里
					var ajaxParam = {
						url: commonUtil.domain() + '/admin/right/user/detail',
						data: {
							userId: userId
						}
					}
					ajaxUtil.post(ajaxParam, function(data) {
						// 如过是修改请求 则账号和密码不允许修改
						$("#user_account").attr("readonly", "readonly");
						$("#user_password").attr("readonly", "readonly");
						formUtil.setForm($('#user_form'), data.data);
						form.render("select");
					});
				}
				// 先查询角色列表 填充到界面
				var ajaxParam = {
					url: commonUtil.domain() + '/admin/right/role/list?status=1'
				}
				if(userId) {
					ajaxParam.data = {
						userId: userId
					}
				}
				ajaxUtil.post(ajaxParam, function(data) {
					var parent = $('#role_checkbox');
					parent.html("");
					var dataList = data.data;
					var childStr;
					for(var i = 0; i < dataList.length; i++) {
						// 没有人可以在添加人员的时候，选择系统管理员角色 系统管理员唯一
						var item = dataList[i];
						if(item.roleId == "super_admin_role") {
							continue;
						}
						var checked = item.checked ? "checked" : "";
						childStr = '<input type="checkbox" lay-filter="resourceCheck" class="role_checkbox" value="' + item.roleId + '" title="' + item.roleName + '"' + checked + '>'
						parent.append($(childStr));
					}
					form.render("checkbox"); // 更新下拉框值
				});
				// 监听表单提交
				form.on('submit(add)', function(data) {
					// 表单提交之前先验证用户名和密码是否合法
					console.info(data);
					var data = data.field;
					if(data.account.length <= 4 || data.account.length > 12) {
						layer.alert("账号长度要大于4个字符，小于12个字符");
						return false;
					}
					// 不给在这儿修改密码
					if(userId) {
						delete data.password;
					} else {
						if(data.password.length <= 8 || data.password.length > 15) {
							layer.alert("密码长度要大于8个字符小于15个字符");
							return false;
						}
					}
					if(!commonUtil.isRegAccout(data.account)) {
						layer.alert("账号仅为数字+字母");
						return false;
					}
					// 提交之前先遍历所勾选的资源
					var roleIdList = [];
					$('.role_checkbox').each(function() {
						var isChecked = $(this).attr('checked');
						console.info($(this));
						if(isChecked) {
							roleIdList.push($(this).val());
						}
					});
					data.roleIds = roleIdList;
					console.info(roleIdList);
					var url, msg;
					// 有userId就是修改，没有userId就是新增
					if(userId) {
						url = commonUtil.domain() + "/admin/right/user/update";
						msg = "修改";
						data.userId = userId;
					} else {
						url = commonUtil.domain() + "/admin/right/user/insert";
						msg = "新增";
					}
					console.log(data);
					var ajaxParam = {
						url: url,
						traditional: true,
						data: data
					}
					console.info(ajaxParam);
					// 发送ajax 提交表单数据
					ajaxUtil.post(ajaxParam, function(data) {
						if(data.code == 200) {
							layer.alert(msg + "成功", {
								icon: 6
							}, function() {
								// 刷新父级的用户表格
								var parentHtml = window.parent;
								parentHtml.userTable.refresh();
								//关闭当前frame
								var index = parent.layer.getFrameIndex(window.name);
								parent.layer.close(index);
							});
						} else {
							layer.alert(data.msg);
						}
					});
					return false;
				});

			});
		</script>
		<script type="text/javascript">
			$(function() {
				commonUtil.setMaxLength(40);
			})
		</script>
	</body>

</html>